package cn.com.guage.dtm.metadata.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import cn.com.guage.dtm.common.annotation.Log;
import cn.com.guage.dtm.common.enums.BusinessType;
import cn.com.guage.dtm.metadata.domain.MdEnv;
import cn.com.guage.dtm.metadata.domain.MdReverseEngineering;
import cn.com.guage.dtm.metadata.domain.MdSys;
import cn.com.guage.dtm.metadata.service.IMdEnvService;
import cn.com.guage.dtm.metadata.service.IMdSysService;
import cn.com.guage.dtm.utils.AESDecrypt;
import cn.com.guage.dtm.utils.AESEncrypt;
import cn.com.guage.dtm.common.core.controller.BaseController;
import cn.com.guage.dtm.common.core.domain.AjaxResult;
import cn.com.guage.dtm.common.utils.poi.ExcelUtil;
import cn.com.guage.dtm.common.core.page.TableDataInfo;

/**
 * 环境管理Controller
 * 
 * @author liujun
 * @date 2021-06-24
 */
@Controller
@RequestMapping("/metadata/env")
public class MdEnvController extends BaseController
{
	private static final Logger log = LoggerFactory.getLogger(MdEnvController.class);
	
    private String prefix = "metadata/env";

    @Autowired
    private IMdEnvService mdEnvService;
    
    @Autowired
    private IMdSysService mdSysService;

    @RequiresPermissions("metadata:env:view")
    @GetMapping()
    public String env(ModelMap mmap)
    {
    	MdSys mdSys=new MdSys();
    	List<MdSys> mdSysList = mdSysService.selectMdSysList(mdSys);
    	mmap.put("mdSysList", mdSysList);
        return prefix + "/env";
    }

    /**
     * 查询环境管理列表
     */
    @RequiresPermissions("metadata:env:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(MdEnv mdEnv)
    {
        startPage();
        List<MdEnv> list = mdEnvService.selectMdEnvList(mdEnv);
        return getDataTable(list);
    }

    /**
     * 导出环境管理列表
     */
    @RequiresPermissions("metadata:env:export")
    @Log(title = "环境管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(MdEnv mdEnv)
    {
        List<MdEnv> list = mdEnvService.selectMdEnvList(mdEnv);
        ExcelUtil<MdEnv> util = new ExcelUtil<MdEnv>(MdEnv.class);
        return util.exportExcel(list, "环境管理数据");
    }

    /**
     * 新增环境管理
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
    	MdSys mdSys=new MdSys();
    	List<MdSys> mdSysList = mdSysService.selectMdSysList(mdSys);
        mmap.put("mdSysList", mdSysList);
        return prefix + "/add";
    }

    /**
     * 新增保存环境管理
     */
    @RequiresPermissions("metadata:env:add")
    @Log(title = "环境管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(MdEnv mdEnv)
    {
    	Long sysId=mdEnv.getSysId();
    	MdSys mdSys = mdSysService.selectMdSysById(sysId);
    	mdEnv.setSysCode(mdSys.getSysCode());
    	mdEnv.setSysName(mdSys.getSysName());
        return toAjax(mdEnvService.insertMdEnv(mdEnv));
    }

    /**
     * 修改环境管理
     */
    @GetMapping("/edit/{envId}")
    public String edit(@PathVariable("envId") Long envId, ModelMap mmap)
    {
        MdEnv mdEnv = mdEnvService.selectMdEnvById(envId);
        AESDecrypt decry=new AESDecrypt(AESEncrypt.ENCRYPASSWORD);
    	String decryPassword=decry.decryptForCBC(mdEnv.getJdbcPassword());
    	mdEnv.setJdbcPassword(decryPassword);
        mmap.put("mdEnv", mdEnv);
        
        MdSys mdSys=new MdSys();
        List<MdSys> mdSysList = mdSysService.selectMdSysList(mdSys);
        mmap.put("mdSysList", mdSysList);
        return prefix + "/edit";
    }

    /**
     * 修改保存环境管理
     */
    @RequiresPermissions("metadata:env:edit")
    @Log(title = "环境管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(MdEnv mdEnv)
    {
    	AESEncrypt encry=new AESEncrypt(AESEncrypt.ENCRYPASSWORD);
    	String encryPassword=encry.encryptForCBC(mdEnv.getJdbcPassword());
    	mdEnv.setJdbcPassword(encryPassword);
        return toAjax(mdEnvService.updateMdEnv(mdEnv));
    }

    /**
     * 删除环境管理
     */
    @RequiresPermissions("metadata:env:remove")
    @Log(title = "环境管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(mdEnvService.deleteMdEnvByIds(ids));
    }
    
    
    @PostMapping("/testdb")
    @ResponseBody
    public AjaxResult testdb(MdEnv mdEnv)
    {
    	try {
			int returnValue = mdEnvService.extractDbMetadata(mdEnv,false);
			log.debug("testdb return:{}", returnValue);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			return AjaxResult.error("数据库连接失败。");
		}
    	return AjaxResult.success("数据库连接成功。");
    }
}
